package com.canbot.u05.activity.admin

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import com.alibaba.fastjson.JSON
import com.canbot.u05.MainApplication
import com.canbot.u05.R
import com.canbot.u05.activity.BaseActivityNoTitle
import com.canbot.u05.activity.loge
import com.canbot.u05.bean.JsonResponse
import com.canbot.u05.socket.IpMsgConst
import com.canbot.u05.utils.OkHttpUtils
import kotlinx.android.synthetic.main.activity_admin_setting.*
import java.util.*

/**
 * desc: 管理员设置界面
 * author: xxl
 * date: 2019/6/19
 */
class AdminSettingActivity : BaseActivityNoTitle() {
    private var adminJson: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_admin_setting)


        rl_admin_password_container.setOnClickListener {
            val intent = Intent(this@AdminSettingActivity, PasswordModifyActivity::class.java)
            intent.putExtra("admin", adminJson)
            startActivity(intent)
        }

        rl_admin_face_container.setOnClickListener {
            val intent = Intent(this@AdminSettingActivity, AdminFaceModifyActivity::class.java)
            intent.putExtra("admin", adminJson)
            startActivity(intent)
        }
    }

    override fun onResume() {
        super.onResume()
        getAdminInfo()
    }


    /**
     * 获取管理员信息
     */
    private fun getAdminInfo() {
        val params = HashMap<String, String>()
        params["adminType"] = "queryAdmin"

        OkHttpUtils.upLoadFile("http://" + IpMsgConst.CHEST_SOCKET_HOST + ":7755/" + "admin", params, object : OkHttpUtils.OkHttpCallBack {
            override fun onSuccess(result: String) {
                loge("result:$result")
                val jsonResponse = JSON.parseObject(result, JsonResponse::class.java)
                if (jsonResponse.rc == -1) {//token无效
                    loge("获取管理员信息失败")
                } else if (jsonResponse.rc == 0) { //token有效
                    loge("获取管理员信息success")
                    adminJson = jsonResponse.result.toString()
                }
            }

            override fun onFailure() = loge("queryAdmin onFailure")
        })
    }

    fun back(view: View) = finish()
}
